Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';

/* 获取引用head.js是传递的参数 */
var gsa = new getScriptArgs();
/* 获取是否显示页面header参数 */
var showHeader = gsa.showHeader;

if (showHeader == 'true') {
	ddlevelsmenu.addEvent(window, function() {
				initOASystem()
			}, "load");
}

/**
 * 初始化系统模块
 */
function initOASystem() {
	document.title = '农六师共青团综合办公系统';

	var span = document.createElement("div");

	document.body.insertBefore(span, document.body.childNodes[0]);

	Ext.MessageBox.wait('系统资源加载过程中...');
	Ext.Ajax.request({
				// 请求地址
				url : context + '/newframe/menujson.jsp',
				// 成功时回调
				success : function(response, options) {
					// 获取响应的json字符串
					Ext.MessageBox.hide();

					var json = response.responseText;
					var o = Ext.util.JSON.decode(json);

					if (o.success) {
						createSystemHeader(span);

						var index = 0;
						var moduleUL = $('ddtopmenubar').childNodes[0];

						/* 循环系统模块 */
						Ext.each(o.list, function(item) {
									index++;

									var li = document.createElement("li");

									if (item.children.length > 0) {
										li.setAttribute('rel', 'ddsubmenu'
														+ index);
										createModuleMenu(item, index);
									}

									var liHTML = (item.murl != ''
											? '<a href="' + item.murl + '">'
													+ item.text + '</a>'
											: '<a href="javascript:void(0);">'
													+ item.text + '</a>');
									li.innerHTML = liHTML;
									moduleUL.appendChild(li);
								});

						ddlevelsmenu.setup("ddtopmenubar", "topbar");
						//var clock = new Clock();
						//clock.display(document.getElementById("clock"));
					} else {
						Ext.Msg.alert('提示', '系统资源加载失败！');
					}
				}
			});
}

/**
 * 创建系统header
 */
function createSystemHeader(span) {
	var txt = '<div style="height:70px;background: url(' + context
			+ '/newframe/images/top-bg.gif) repeat-x;">';
	txt += '<div style="position:absolute;left:10px;top:2px;padding:2px;">';
	txt += '<img src="' + context
			+ '/newframe/images/logo.gif" width="488" height="61" />';
	txt += '</div>';
	txt += '<div style="position:absolute;right:100px;top:35px;padding:5px;font-size: 15px;color:#fff;">';
	txt += '&nbsp;<font style="font-weight:bold;font-size:13px;">用户:</font> ' + userName;
	txt += '&nbsp;<font style="font-weight:bold;font-size:13px;">部门:</font> ' + groupName;
	txt += ' <span id="clock"></span>';
	txt += ' </div>';
	txt += '<div style="position:absolute;right:10px;top:30px;padding:5px;font-size: 14px;color:#fff;">';
	txt += ' <a href="javascript:void(0);" target="_self" onClick="logout();"><img style="vertical-align:middle;" src="'
			+ context
			+ '/newframe/images/logout.png" alt="退出系统" width="25" height="25" border="0"></a>';
	txt += '</div>';
	txt += '</div>';
	txt += '<div id="ddtopmenubar" class="mattblackmenu">';
	txt += '<ul>';
	txt += '<li><a href="javascript:void(0);" onClick="switchDesktop();">首&nbsp;&nbsp;&nbsp;&nbsp;页</a></li>';
	txt += '</ul>';
	txt += '</div>';
	// txt += '<div id="navi">&nbsp;当前位置: 个人桌面</div>';

	// span.style.cssText = 'position:fixed;z-index:1000;';
	span.innerHTML = txt;
}

/**
 * 创建系统模块一级菜单
 */
function createModuleMenu(item, index) {
	if (item.children.length > 0) {
		var menuUL = document.createElement("ul");
		menuUL.id = 'ddsubmenu' + index;
		menuUL.className = 'ddsubmenustyle';

		/* 循环模块菜单 */
		Ext.each(item.children, function(itemMenu) {
			var menuLI = document.createElement("li");

			var liHTML = (itemMenu.mnurl != ''
					? '<a href="javascript:void(0);" onclick="javascript:switchMenu(\''
							+ itemMenu.mnurl
							+ '\');">'
							+ itemMenu.text
							+ '</a>'
					: '<a href="javascript:void(0);">' + itemMenu.text + '</a>');

			menuLI.innerHTML = liHTML;
			createModuleChildMenu(itemMenu, menuLI);
			menuUL.appendChild(menuLI);
		});

		insertAfter(menuUL, $('ddtopmenubar'));
	}
}

/**
 * 创建一级菜单的子菜单
 * 
 * @param {}
 *            item
 */
function createModuleChildMenu(item, parentMenu) {
	if (item.children.length > 0) {
		var menuUL = document.createElement("ul");

		/* 循环模块菜单 */
		Ext.each(item.children, function(itemMenu) {
			var menuLI = document.createElement("li");

			var liHTML = (itemMenu.mnurl != ''
					? '<a href="javascript:void(0);" onclick="javascript:switchMenu(\''
							+ itemMenu.mnurl
							+ '\');">'
							+ itemMenu.text
							+ '</a>'
					: '<a href="javascript:void(0);">' + itemMenu.text + '</a>');

			menuLI.innerHTML = liHTML;
			menuUL.appendChild(menuLI);

			if (itemMenu.children.length > 0) {
				createModuleChildMenu(itemMenu, menuLI);
			}
		});

		parentMenu.appendChild(menuUL);
	}
}

/**
 * 切换菜单
 * 
 * @param {}
 *            url 菜单链接
 */
function switchMenu(url) {
	// 窗口的可视高度
	var windowHeight = document.all
			? document.getElementsByTagName("html")[0].offsetHeight
			: window.innerHeight;

	// 页面的内容高度
	var pageHeight = Math.max(windowHeight, document
					.getElementsByTagName("body")[0].scrollHeight);

	var frmHeight = windowHeight - 101-5;

	$('desktopTbl').style.display = 'none';
	$('frameTbl').style.display = 'block';
	resizeFrame(frmHeight);
	$('menuFrm').src = context + url;
}

function resizeFrame(h) {
	$('menuFrm').height = h;
}

function switchDesktop() {
	initDesktop();
	$('desktopTbl').style.display = 'block';
	$('frameTbl').style.display = 'none';
	$('menuFrm').src = context + '/system/blank.jsp';
}

/**
 * 退出系统
 */
function logout() {
	if (confirm("您确定要退出系统吗？")) {
		top.document.location = context + "/system/logout.do";
	}
}